package com.bestv.search.management.delegate;

import com.bestv.search.common.cache.SiteCache;
import com.bestv.search.common.cache.SystemCache;
import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.dataSource.DataSourceKeyUtil;
import com.bestv.search.common.service.SiteManager;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class SiteDelegate implements AfterReturningAdvice {
    private SiteManager siteManager;
    private SystemCache systemCache;

    public SiteManager getSiteManager() {
        return siteManager;
    }

    public void setSiteManager(SiteManager siteManager) {
        this.siteManager = siteManager;
    }

    public SystemCache getSystemCache() {
        return systemCache;
    }

    public void setSystemCache(SystemCache systemCache) {
        this.systemCache = systemCache;
    }

    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        if (method.getName().startsWith("save") || method.getName().startsWith("delete")) {
            DataSourceKey key = DataSourceKeyUtil.convert(systemCache.getLocalDataSourceKey());
            SiteCache.initMap(key, siteManager);
        }
    }
}
